package cn.kgc.crowdfundingadminspringboot2.service;

import cn.kgc.crowdfundingadminspringboot2.common.CrowdFundingUtils;
import cn.kgc.crowdfundingadminspringboot2.mapper.AdminMapper;
import cn.kgc.crowdfundingadminspringboot2.pojo.Admin;
import cn.kgc.crowdfundingadminspringboot2.pojo.AdminExample;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdminServiceimpl implements  AdminService {
    @Resource
    AdminMapper adminMapper;
    @Override
    public Admin selectById(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    @Override
    public Admin login(String loginAcct, String userPawd) {
        Admin admin=null;
        AdminExample adminExample=new AdminExample();
        AdminExample.Criteria criteria=adminExample.createCriteria();
        criteria.andLoginacctEqualTo(loginAcct);

        List<Admin> admins = adminMapper.selectByExample(adminExample);
        if(CrowdFundingUtils.collectionEffective(admins)){
            admin=admins.get(0);
            String md5=CrowdFundingUtils.md5(userPawd);

            if(md5.equals(admin.getUserpswd())){
                return admin;
            }else {
                admin=null;
            }
        }


        return admin;
    }

    @Override
    public PageInfo<Admin> queryForKeywordSearch(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("id desc");
        PageInfo<Admin> pageInfo=new PageInfo<>();
        if(CrowdFundingUtils.stringEffective(keyword)){
            AdminExample example=new AdminExample();
            AdminExample.Criteria criteria = example.createCriteria();
            AdminExample.Criteria criteria1 = example.createCriteria();
            AdminExample.Criteria criteria2 = example.createCriteria();
            criteria.andLoginacctLike("%"+keyword+"%");
            criteria2.andUsernameLike("%"+keyword+"%");
            criteria1.andEmailLike("%"+keyword+"%");
            example.or(criteria1);
            example.or(criteria2);
            List<Admin> admins = adminMapper.selectByExample(example);
            pageInfo = new PageInfo<>(admins);
        }else{
            List<Admin> admins = adminMapper.selectByExample(null);
           pageInfo = new PageInfo<>(admins);

        }
        return pageInfo;
    }

    @Override
    public void batchRemove(List<Integer> adminIdList) {
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria= example.createCriteria();
        criteria.andIdIn(adminIdList);
       adminMapper.deleteByExample(example);
    }

    @Override
    public void saveAdmin(Admin admin) {

        CrowdFundingUtils.md5(admin.getUserpswd());

        adminMapper.insert(admin);
    }

    @Override
    public Admin getAdminById(Integer adminId) {
        return adminMapper.selectByPrimaryKey(adminId);
    }

    @Override
    public void updateAdmin(Admin admin) {

        if(CrowdFundingUtils.stringEffective(admin.getUserpswd())){
           String Userpswd= admin.getUserpswd();
           CrowdFundingUtils.md5(Userpswd);
           admin.setUserpswd(Userpswd);
        }
        adminMapper.updateByPrimaryKeySelective(admin);
    }
}
